package com.example.chapter06;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.chapter06.database.ShoppingDBHelper;
import com.example.chapter06.entity.CartInfo;
import com.example.chapter06.entity.GoodsInfo;
import com.example.chapter06.util.ToastUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiConsumer;

public class ShoppingCartActivity extends AppCompatActivity implements View.OnClickListener {

    private LinearLayout ll_cart;
    private ShoppingDBHelper mDBHelper;
    //声明一个购物车中的商品信息列表
    private List<CartInfo> mCartList;

    //声明一个根据商品编号查找商品信息的映射，把商品信息缓存起来，这样不用每一次都去查数据库
    private final Map<Integer, GoodsInfo> mGoodsMap = new HashMap<>();
    private TextView tv_total_price;
    private TextView tv_count;
    private LinearLayout ll_empty;
    private LinearLayout ll_content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping_cart);

        TextView tv_title = findViewById(R.id.tv_title);
        tv_title.setText("购物车");

        tv_count = findViewById(R.id.tv_count);
        tv_count.setText(String.valueOf(MyApplication.getInstance().goodsCount));

        ll_cart = findViewById(R.id.ll_cart);
        tv_total_price = findViewById(R.id.tv_total_price);
        ll_empty = findViewById(R.id.ll_empty);
        ll_content = findViewById(R.id.ll_content);

        findViewById(R.id.iv_back).setOnClickListener(this);
        findViewById(R.id.btn_clear).setOnClickListener(this);
        findViewById(R.id.btn_settle).setOnClickListener(this);
        findViewById(R.id.btn_shopping_channel).setOnClickListener(this);

        /*此时不用打开读写连接，因为主界面没有消失，数据库一直在连接*/
        mDBHelper = ShoppingDBHelper.getInstance(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        showCart();
    }

    //展示购物车中的商品列表
    private void showCart() {
        if(MyApplication.getInstance().goodsCount == 0){
            ll_empty.setVisibility(View.VISIBLE);
            ll_content.setVisibility(View.GONE);
            return;
        }
        //移除下面的所有子视图
        ll_cart.removeAllViews();
        //查询购物车数据库中所有的商品记录
        mCartList = mDBHelper.queryAllCartInfo();
        if (mCartList.isEmpty()) {
            return;
        }


        for (CartInfo info : mCartList) {
            Log.d("liu",info.toString());
            //根据商品编号查询商品数据库中的商品记录
            GoodsInfo goods = mDBHelper.queryGoodsInfoById(info.getGoods_id());

            //缓存商品信息
            mGoodsMap.put(info.getGoods_id(), goods);

            //获取布局文件item_cart.xml的根视图
            View view = LayoutInflater.from(this).inflate(R.layout.item_cart, null);
            ImageView iv_thumb = view.findViewById(R.id.iv_thumb);
            TextView tv_name = view.findViewById(R.id.tv_name);
            TextView tv_description = view.findViewById(R.id.tv_description);
            TextView tv_count = view.findViewById(R.id.tv_item_count);
            TextView tv_price = view.findViewById(R.id.tv_price);
            TextView tv_sum = view.findViewById(R.id.tv_sum);

            /*错在view.findViewById 忘记写view了*/

            /*
            *  // 获取布局文件item_cart.xml的根视图
            View view = LayoutInflater.from(this).inflate(R.layout.item_cart, null);
            ImageView iv_thumb = view.findViewById(R.id.iv_thumb);
            TextView tv_name = view.findViewById(R.id.tv_name);
            TextView tv_desc = view.findViewById(R.id.tv_desc);
            TextView tv_count = view.findViewById(R.id.tv_count);
            TextView tv_price = view.findViewById(R.id.tv_price);
            TextView tv_sum = view.findViewById(R.id.tv_sum);
            */

            iv_thumb.setImageURI(Uri.parse(goods.getPicPath()));
            tv_name.setText(goods.getName());
            tv_description.setText(goods.getDescription());
            tv_count.setText(String.valueOf(info.getCount()));
            tv_price.setText(String.valueOf(goods.getPrice()));
            tv_sum.setText(String.valueOf(goods.getPrice() * info.getCount()));

            //给商品行添加长按事件，长按商品行就删除该商品
            view.setOnLongClickListener(v->{
                AlertDialog.Builder builder = new AlertDialog.Builder(ShoppingCartActivity.this);
                builder.setMessage("是否从购物车删除"+goods.getName()+"?");
                builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //移除当前视图
                        ll_cart.removeView(v);
                        //删除该商品
                        deleteGoods(info);
                    }
                });
                builder.setNegativeButton("否",null);
                builder.create().show();
                return true;
            });

            //给商品行添加点击事件。点击商品行跳到商品的详情页
            view.setOnClickListener(v->{
                Intent intent = new Intent(ShoppingCartActivity.this,ShoppingDetailActivity.class);
                intent.putExtra("goods_id",info.getGoods_id());
                startActivity(intent);
            });

            //往购物车列表添加商品行
            /*这里不需要设置参数：因为条目的宽高都设计好了*/
            ll_cart.addView(view);


        }
        //重新计算购物车中的商品总金额
        refreshTotalPrice();
    }

    private void deleteGoods(CartInfo info) {
        MyApplication.getInstance().goodsCount -= info.getCount();
        //从购物车的数据库中删除商品
        mDBHelper.deleteCartInfoByGoodsId(info.getGoods_id());
        //从购物车列表中删除商品
        /**/
        CartInfo removed = null;
        for (CartInfo cartInfo : mCartList) {
            if(cartInfo.getGoods_id() == info.getGoods_id()){
                removed = cartInfo;
                break;
            }
        }
        mCartList.remove(removed);
        //显示最新的商品数量
        showCount();
        ToastUtil.show(this,"已从购物车中删除"+ Objects.requireNonNull(mGoodsMap.
                get(info.getGoods_id())).getName());
        mGoodsMap.remove(info.getGoods_id());
        //刷新购物车中所有商品的总金额
        refreshTotalPrice();
    }

    //显示购物车图表中的商品数量
    private void showCount() {
        tv_count.setText(String.valueOf(MyApplication.getInstance().goodsCount));
        //购物车中没有商品，显示空空如也
        if(MyApplication.getInstance().goodsCount == 0){
            ll_empty.setVisibility(View.VISIBLE);
            ll_content.setVisibility(View.GONE);
            ll_cart.removeAllViews();
        }else {
            ll_content.setVisibility(View.VISIBLE);
            ll_empty.setVisibility(View.GONE);
        }
    }

    //重新计算购物车中的商品总金额
    private void refreshTotalPrice() {
        float totalPrice = 0;
        for (CartInfo info : mCartList) {
            totalPrice += info.getCount() * Objects.requireNonNull(mGoodsMap.get(info.getGoods_id())).getPrice();
        }
        tv_total_price.setText(String.valueOf(totalPrice));

    }


    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.iv_back){
            finish();
        }
        if(v.getId() ==R.id.btn_clear){
            //清空购物车
            //删除数据库中的所有数据
            mDBHelper.deleteAllCartInfo();
            //将购物车上的数字置为0
            MyApplication.getInstance().goodsCount = 0;
            //展示最新的商品数量
            showCount();
            //更新总金额
            refreshTotalPrice();
        }
        if(v.getId() == R.id.btn_shopping_channel){
            //从购物车页面跳转到商场页面
            Intent intent = new Intent(this, ShoppingChannelActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }

        if(v.getId() == R.id.btn_settle){
          //点击了"结算"按钮
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("结算商品");
            builder.setMessage("客观抱歉，支付功能尚未开通，请下次再来");
            builder.setPositiveButton("我知道了",null);
            builder.create().show();
        }
    }
}